package com.zry.fertilizer.ui.activity


import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.zry.fertilizer.R
import com.zry.fertilizer.adapter.ProductListAdapter
import com.zry.fertilizer.animation.RecyclerViewUtilKt
import com.zry.fertilizer.been.*
import com.zry.fertilizer.databinding.ActivityProductListBinding
import com.zry.fertilizer.retrofit.HttpServer
import com.zry.fertilizer.retrofit.ShowUserView
import com.zry.fertilizer.ui.base.BaseActivity
import com.zry.fertilizer.utils.ACacheUtil
import kotlinx.android.synthetic.main.activity_product_list.*
import kotlinx.android.synthetic.main.common_title.*

class ProductListActivity : BaseActivity(), ShowUserView {
    private var productInfoListBinding: ActivityProductListBinding? = null
    private var productList: MutableList<ProductNameBean>? = null
    private var productCacheList: MutableList<ProductNameBean>? = null
    private var productListAdapter: ProductListAdapter? = null
    private var title = ""
    override fun getLayoutId(): Int {
        return R.layout.activity_product_list
    }

    override fun initData() {


        title = intent.getStringExtra("title")
        titleCenterText.text = "产品列表"
        commonLeftLinearLayout.setOnClickListener { finish() }

        productInfoListBinding = viewDataBinding as ActivityProductListBinding
        productInfoListBinding?.handlerClick = this

        productList = mutableListOf()
        initRecycler()
        initEtView()
    }

    private fun initRecycler() {
        HttpServer(this).getProductByProductNameList(ACacheUtil().getLoginInfo().EntID, "", 1, 999, this)
        recyclerViewUtilKt = RecyclerViewUtilKt(this, productRecycler)
        recyclerViewUtilKt?.initRecyclerView()
        productListAdapter = ProductListAdapter(this, R.layout.item_product_list, productList)
        recyclerViewUtilKt?.setAdapter(productListAdapter!!)
        productListAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this@ProductListActivity, ProductInfoActivity::class.java)
            intent.putExtra("productNameBean", productList!![position])
            startActivity(intent)
        }
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            27 -> {
                val resultData = data as ResultBean<MutableList<ProductNameBean>>
                if (resultData.State == "1") {
                    productCacheList = resultData.ResultData
                    productList!!.removeAll(productList!!)
                    productList!!.addAll(resultData.ResultData)
                    productListAdapter?.notifyDataSetChanged()
                } else {
                    toast(resultData.Message)
                }
            }
        }
    }

    fun searchProductMethod(view: View) {
        when (view.id) {
            R.id.searchProductBtn -> {
                val productName = searchProductNameET.text.toString().trim()
                HttpServer(this).getProductByProductNameList(ACacheUtil().getLoginInfo().EntID, productName, 1, 999, this)
            }
        }
    }

    private fun initEtView() {
        searchProductNameET.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) = Unit
            override fun afterTextChanged(p0: Editable?) {
                val searchProduct = searchProductNameET.text.toString().trim()
                productList!!.removeAll(productList!!)
                productList!!.addAll(getProductList(searchProduct))
                productListAdapter?.notifyDataSetChanged()
            }
        })
    }

    fun getProductList(searchProduct: String): MutableList<ProductNameBean> {
        val productBeanList: MutableList<ProductNameBean> = mutableListOf()
        productCacheList!!.forEach {
            if (it.ProductName.contains(searchProduct)) {
                productBeanList.add(it)
            }
        }
        return productBeanList
    }
}
